package com.aier.school.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.Constant;
import com.aier.common.common.ImageUtil;
import com.aier.school.entity.SchoolPhoto;
import com.aier.school.service.ISchoolService;

@Scope("prototype")
@Component("schoolAlbumAction")
public class SchoolAlbumAction extends SchoolBaseAction {
    /**
     * 学校相册
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;

    private List<SchoolPhoto> spList; // 学校相册图片列表
    private SchoolPhoto sp; // 班级相册
    private Long spid;// 图片id

    private int s; // 成功:1 ,失败:0
    private String error_info; // 错误提示信息

    private Integer sid; // 学校id

    private File file2;// 照片文件
    private String photoName;// 照片原名
    private String img;// 班级相册图片名

    @Override
    public String execute() {

        return SUCCESS;
    }

    // 查询相册照片
    // 查询某相册下的图片列表
    public String searchSchoolPhotoImg() {
        spList = schoolService.searchSchoolPhotoList(sid);
        return SUCCESS;
    }

    // 上传相册图片到服务器
    public String uploadSchoolPhotoImg() {
        img = ImageUtil.uploadImageAdjustable(file2, schoolgal_upload_url,
                photoName, Constant.num_200, Constant.num_200,
                Constant.num_500, Constant.num_500, false);
        return SUCCESS;
    }

    // 删除上传的照片
    public String deleteUploadImg() {
        File fo1 = new File(schoolgal_upload_url + "big/" + img);
        File fo2 = new File(schoolgal_upload_url + "origin/" + img);
        File fo3 = new File(schoolgal_upload_url + "small/" + img);
        if (fo1.exists()) {
            fo1.delete();
        }
        if (fo2.exists()) {
            fo2.delete();
        }
        if (fo3.exists()) {
            fo3.delete();
        }
        s = 1;
        return SUCCESS;
    }

    // 上传图片到相册
    public String addUserPhotoImg() {
        sp = new SchoolPhoto();
        sp.setSid(sid);
        sp.setImg(img);
        spid = schoolService.insertSchoolPhoto(sp);
        s = 1;
        return SUCCESS;
    }

    // 删除相册图片
    public String deleteSchoolPhoto() {
        s = schoolService.deleteSchoolPhoto(spid);
        return SUCCESS;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public List<SchoolPhoto> getSpList() {
        return spList;
    }

    public void setFile2(File file2) {
        this.file2 = file2;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public SchoolPhoto getSp() {
        return sp;
    }

    public void setSp(SchoolPhoto sp) {
        this.sp = sp;
    }

    public Long getSpid() {
        return spid;
    }

    public void setSpid(Long spid) {
        this.spid = spid;
    }
}
